package cn.dinghu.pro.controller;


import cn.dinghu.pro.common.ResultData;
import cn.dinghu.pro.entity.Orders;
import cn.dinghu.pro.service.IOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author xibao
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/orders")
@Api(tags = "订单管理")
@ApiSupport(author = "熙神" )
public class OrdersController {
    @Autowired
    private IOrdersService orderService;

    @GetMapping("/getPage")
    @ApiOperation(value = "获取分页数据",notes = "使用mp实现")
    public ResultData getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                              String nurseId, String userId , String orderStatus, String category,String phone){
        IPage<Orders> dictPage= new Page<>(pageNo, pageSize);
        // 构造查询条件
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        // 判断 typeCode不为空的时候，进行调节附加
        if(StringUtils.isNotEmpty(nurseId)){
            int x=Integer.parseInt(nurseId);
            queryWrapper.eq("nurse_id",x);
        }

        if(StringUtils.isNotEmpty(userId)){
            int x=Integer.parseInt(userId);
            queryWrapper.eq("user_id",x);
        }

        if(StringUtils.isNotEmpty(orderStatus)){
            int x=Integer.parseInt(orderStatus);
            queryWrapper.eq("order_status",x);
        }

        if(StringUtils.isNotEmpty(category)){
            int x=Integer.parseInt(category);
            queryWrapper.eq("category",x);
        }

        if(StringUtils.isNotEmpty(category)){
            queryWrapper.eq("phone",phone);
        }

        IPage<Orders> page = orderService.page(dictPage,queryWrapper);
        return ResultData.ok(page);
    }

    @DeleteMapping("/delete/{orderId}")
    @ApiOperation(value = "删除订单",notes = "使用mp实现")
    public ResultData delete(@PathVariable int orderId){
        return orderService.removeById(orderId)==true?ResultData.ok():ResultData.error();
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新订单",notes = "使用mp实现")
    public ResultData update( Orders orders){
        return orderService.edit(orders);
    }

    @GetMapping("/get/{orderId}")
    @ApiOperation(value = "获取订单",notes = "使用mp实现")
    public ResultData get(@PathVariable int orderId){
        return orderService.getById(orderId)==null?ResultData.noData():ResultData.ok(orderService.getById(orderId));
    }

    @PutMapping("/updataStatus")
    @ApiOperation(value = "更新订单状态",notes = "使用mp实现")
    public ResultData updateStatus(@RequestParam int status,@RequestParam int id){
        return orderService.updateStatus(status,id);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取订单列表",notes = "使用mp实现")
    public ResultData list(Integer userId,String nurseName, String userName, String orderStatus, String category, String phone,
                           @RequestParam Integer pageNo, @RequestParam Integer pageSize){
        return ResultData.ok(orderService.getPageInfo(userId,nurseName,userName,orderStatus,category,phone,pageNo,pageSize));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加订单",notes = "使用mp实现")
    public ResultData add(Orders orders){
        return orderService.add(orders);
    }

    @PutMapping("/updatepj")
    @ApiOperation(value = "更新评价",notes = "使用mp实现")
    public ResultData updatepj(int orderId, int pj){
        return orderService.updatePj(orderId,pj)>0?ResultData.ok_201():ResultData.fail_201();
    }

}
